<template>
  <!-- <div>
    <el-button type="primary" :icon="Plus" @click="emit('update:modelValue', STATUS.SPUFORM)">添加SPU</el-button>
    <el-button type="primary" :icon="Plus" @click="emit('update:modelValue', STATUS.SKUFORM)">添加SKU - 假的</el-button>
  </div> -->
  <div>
    <el-button class="mb-10" type="primary" :icon="Plus" @click="emit('update:modelValue', STATUS.SPUFORM)" :disabled="!categoryStore.category3Id">添加SPU</el-button>

    <el-table :data="spuList" border class="mb-10">
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作" width="240">
        <template #default="{row,$index}">
          <el-button type="success" size="small" :icon="Plus" @click="addSku(row)" title="新增SKU"></el-button>
          <el-button type="warning" size="small" :icon="Edit" title="修改SPU" @click="editSpu(row)"></el-button>
          <el-button type="info"    size="small" :icon="InfoFilled" @click="showSkuList(row)" title="查看SKU列表"></el-button>
          <el-popconfirm :title="`确认要删除[${ row.spuName }]吗?`" @confirm="deleteSpu(row)">
            <template #reference>
              <el-button type="danger"  size="small" :icon="Delete" title="删除SPU"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="page"
      v-model:page-size="limit"
      :total="total"
      :page-sizes="[3, 6, 9]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="80%"
    >
      <el-table :data="skuList" stripe>
        <el-table-column label="名称" prop="skuName"/>
        <el-table-column label="价格" prop="price"/>
        <el-table-column label="重量" prop="weight"/>
        <el-table-column label="默认图片">
          <template #default="{ row, $index }">
            <img :src="row.skuDefaultImg" style="width: 60px;height: 60px;">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>


  </div>
</template>

<script setup lang="ts">
import useCategoryStore from '@/stores/category';
import { Delete, Edit, Plus, InfoFilled } from '@element-plus/icons-vue'
import { ref, watch } from 'vue';
import { STATUS } from '../../index.vue'
import spuApi ,{type SpuModel} from '@/api/spu'
import { ElMessage } from 'element-plus';
import skuApi, { type SkuModel } from '@/api/sku';
// defineProps<{
//   modelValue: number
// }>()
const emit = defineEmits<{
  (e: 'update:modelValue', status: number): void
  (e: 'changeSpuInfo', spu: SpuModel): void
}>()

// 删除Sku
const deleteSpu = async (row:SkuModel)=>{
  try {
    await spuApi.deleteSpu(row.id!)
    ElMessage.success('删除成功')
    getPage()
  } catch (error) {
    ElMessage.error('删除失败，请重试')
  }
}


// 记录标题
const dialogTitle = ref('')
// 用于记录是否展示 dialog , 默认为隐藏
const dialogVisible = ref(false)
// 记录需要展示的数据
const skuList = ref<SkuModel[]>([])
const showSkuList = async (row:SpuModel)=>{
  try {
    let result = await skuApi.findBySpuId(row.id!)
    skuList.value = result

    dialogVisible.value = true    // 显示弹框

    dialogTitle.value = `[${ row.spuName }]的SKU列表`

  } catch (error) {
    ElMessage.error('获取sku列表失败,请重试')
  }
}





// 新增sku
const addSku = (row:SpuModel)=>{
  emit('update:modelValue',STATUS.SKUFORM)
  emit('changeSpuInfo',row)
}

const editSpu = (row:SpuModel)=>{
  emit('update:modelValue',STATUS.SPUFORM)
  emit('changeSpuInfo',row)
}


const page = ref(1)
const limit = ref(3)
const total = ref(0)

const categoryStore = useCategoryStore()

// 存储spu列表
const spuList = ref<SpuModel[]>([])

const handleSizeChange = (val: number) => {
  // 组装数据
  limit.value = val;
  // 发送请求
  getPage();
}
// 翻页的回调
const handleCurrentChange = (val: number) => {
  // console.log(1111111, val)
  // 组装数据
  page.value = val;
  // 发送请求
  getPage();
}

// 获取spu列表
const getPage = async ()=>{
 try {
  let result = await spuApi.getPage(page.value,limit.value,categoryStore.category3Id as number)
  spuList.value = result.records
  total.value = result.total
 } catch (error) {
  ElMessage.error('获取SPU列表数据失败，请重试')
 }
}

watch(()=>categoryStore.category3Id,(nval)=>{
  if (nval) {
    getPage()
  }else{
    // 重置数据
    spuList.value = []
  }
},{immediate:true})
</script>

<style scoped>

</style>